package com.cloud_notes.demo.common.interceptor;

import com.cloud_notes.demo.common.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;


@Slf4j
@Component
public class UserLoginInterceptor implements HandlerInterceptor {
    //拦截器，对token进行校验

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //先获取token
        String token = request.getHeader("user_header_token");
        log.info("用户token:{}",token);
//        //校验是否存在
//        if(!StringUtils.hasLength(token)){
//            //没有token
//            return false;
//        }
        Claims claims = JwtUtil.parseJWT(token);
        if(claims == null){
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
